package MyGraph;

import java.util.Scanner;

public class MGraph implements IGraph{

    // 类别 UDG 无向图, DG 有向图, UDN 无向网, DN 有向网
    public enum graphKind{UDG, DG, UDN, DN};
    // 图的种类
    private graphKind kind;
    // 图的顶点数
    private int vNum;
    // 图的边数
    private int eNum;
    // 顶点
    private Object[] v;
    // 邻接矩阵
    private int[][] e;

    // 构造函数
    public MGraph() {
        this(null, 0, 0, null, null);
    }

    public MGraph(graphKind kind) {
        this(kind, 0, 0, null, null);
    }

    public MGraph(graphKind kind, int vNum, int eNum, Object[] v, int[][] e) {
        this.kind = kind;
        this.vNum = vNum;
        this.eNum = eNum;
        this.v = v;
        this.e = e;
    }


    // 创建无向图
    private void createUDG(){
        Scanner sc = new Scanner(System.in);
        vNum = sc.nextInt();
        eNum = sc.nextInt();
        v = new Object[vNum];
        // 构造顶点集
        for(int i=0; i<vNum; i++){
            v[i] = sc.next();
        }
        // 构造边集
        e = new int[vNum][eNum];
        for(int i=0; i<eNum; i++){
            int m = locateVex(sc.next());
            int n = locateVex(sc.next());
            e[m][n] = e[n][m]=1;
        }
    }

    // 创建有限图
    private void createDG(){
        Scanner sc = new Scanner(System.in);
        vNum = sc.nextInt();
        eNum = sc.nextInt();
        v = new Object[vNum];
        for(int i=0; i<vNum; i++){
            v[i] = sc.next();
        }
        e = new int[vNum][eNum];
        for(int i=0; i<eNum; i++){
            int m = locateVex(sc.next());
            int n = locateVex(sc.next());
            e[m][n] = 1;
        }
    }

    // 创建无向网
    private void createUDN(){
        Scanner sc = new Scanner(System.in);
        vNum = sc.nextInt();
        eNum = sc.nextInt();

        v=new Object[vNum];

        // 构造顶点集
        for(int i=0; i<vNum; i++){
            v[i]=sc.next();
        }

        // 初始化边集
        e = new int[vNum][eNum];
        for(int i=0; i<vNum; i++){
            for(int j=0; j<vNum; j++){
                e[i][j] = Integer.MAX_VALUE;
            }
        }

        // 构造边集
        for(int i=0; i<eNum; i++){
            int m = locateVex(sc.next());
            int n = locateVex(sc.next());
            e[m][n] = e[n][m] = sc.nextInt();
        }
    }


    // 创建有向网
    private void createDN(){
        Scanner sc = new Scanner(System.in);
        vNum = sc.nextInt();
        eNum = sc.nextInt();
        // 构造顶点集
        v= new Object[vNum];
        for(int i=0; i<vNum; i++){
            v[i] = sc.next();
        }

        // 初始化边集
        e = new int[vNum][eNum];
        for(int i=0; i<vNum; i++){
            for(int j=0; j<vNum; j++){
                e[i][j] = Integer.MAX_VALUE;
            }
        }

        // 构造边集
        for(int i=0; i<eNum; i++){
            int m = locateVex(sc.next());
            int n = locateVex(sc.next());
            e[m][n]=sc.nextInt();
        }
    }


    @Override
    public void createGraph() {

    }

    @Override
    public int getVNum() {
        return 0;
    }

    @Override
    public int getENum() {
        return 0;
    }

    @Override
    public Object getVex(int x) {
        return null;
    }

    // 顶点的下标
    public int locateVex(Object x) {
        for(int i=0; i<vNum; i++){
            if(v[i].equals(x)){
                return i;
            }
        }
        return -1;
    }

    // 查找第一个邻接点
    public int firstAdj(int i) throws Exception {
        if(i<0 || i>=vNum){
            throw new Exception("该顶点不存在");
        }
        for(int j=0; j<vNum; j++){
            if(e[i][j]!=0 && e[i][j]< Integer.MAX_VALUE){
                return j;
            }
        }
        return -1;
    }

    // 查找下一个邻接点
    public int nextAdj(int i, int j) {
        if(j==vNum-1){
            return -1;
        }
        for(int k=j+1; k<vNum; k++){
            if(e[i][k]!=0 && e[i][k]<Integer.MAX_VALUE){
                return k;
            }
        }
        return -1;
    }


}
